package com.example.demo2;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.demo2.activity.LoginActivity;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initWidget();
    }

    private void initWidget() {

        Button button = findViewById(R.id.btn_login);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                Log.i("MainActivity", "onClick: ");

                //requescode，你自定义的一个int类型的数值（一般>0）,当从activityB中返回来的时候。
                //会携带回来。所以你可以用这个参数来判断是从哪个activity中返回的。
                //（前提是你在开启新的activity的时候 要传的requestcode不一样）
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Log.i("onActivityResult", "requestCode: " + requestCode + ",resultCode: " + resultCode + ",data: " + data);

        if (1 != requestCode || RESULT_OK != resultCode || null == data) {
            return;
        }

        //取出用户名和密码
        String username = data.getStringExtra("username");

        String password = data.getStringExtra("password");

        showResult(username, password);
    }

    private void showResult(String username, String password) {
        Button buttonLogin = findViewById(R.id.btn_login);
        buttonLogin.setVisibility(View.GONE);


        LinearLayout linearLayout = findViewById(R.id.user_info);
        linearLayout.setVisibility(View.VISIBLE);

        TextView txtUsername = findViewById(R.id.txt_username);
        TextView txtPassword = findViewById(R.id.txt_password);

        txtUsername.setText(username);
        txtPassword.setText(password);

    }
}